package com.example.testapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.entity.Order;
import com.example.testapplication.entity.Result;
import com.example.testapplication.entity.ShoppingMsgEntity;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.OptionalDouble;

public class OrderMsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context mContext;
    private List<Order> listDatas;

    public void setListDatas(List<Order> listDatas) {
        this.listDatas = listDatas;
    }

    public OrderMsgAdapter(Context context) {
        this.mContext = context;
    }
    public OrderMsgAdapter(Context context, List<Order> datas) {
        this.listDatas=datas;
        this.mContext = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_order,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;
        Order order = listDatas.get(position);
        vh.tvConsignee.setText(order.getConsignee());
        vh.tvOrderTime.setText(order.getSaletime());
        vh.tvPrice.setText(String.valueOf(order.getPrice()));
        vh.tvAddress.setText(order.getAddress());
        vh.tvSumPrice.setText(String.valueOf(order.getPrice()*order.getNum()));
        vh.tvShoppingName.setText(order.getGoodname());
        vh.tvNum.setText(String.valueOf(order.getNum()));
        if(order.getState()==1){
            vh.btnPay.setEnabled(false);
        }
        vh.btnPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pay(order);
            }
        });
    }

    private void pay(Order order) {
        HashMap<String,Object> params = new HashMap<String,Object>();
        params.put("id", order.getId());
        Api.config(ApiConfig.USER_PAY,params).putRequest(mContext,new CallBack() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Result res = gson.fromJson(result, Result.class);
                if(res.getCode() == 1){

                }else{

                }
            }

            @Override
            public void onFailure(IOException e) {

            }
        });
    }

    @Override
    public int getItemCount() {
        if(listDatas!=null&&listDatas.size()>0)
        {
            return listDatas.size();
        }else{
            return 0;
        }

    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvShoppingName;
        private TextView tvPrice;
        private TextView tvNum;
        private TextView tvSumPrice;
        private TextView tvAddress;
        private TextView tvOrderTime;
        private TextView tvConsignee;
        private Button btnPay;
        public ViewHolder(@NonNull View view) {
            super(view);
            tvNum = view.findViewById(R.id.order_num);
            tvShoppingName = view.findViewById(R.id.order_shopping_name);
            tvPrice = view.findViewById(R.id.order_shopping_price);
            tvSumPrice = view.findViewById(R.id.order_price);
            tvAddress = view.findViewById(R.id.shopping_description);
            tvOrderTime = view.findViewById(R.id.order_time);
            tvConsignee = view.findViewById(R.id.order_consignee);
            btnPay = view.findViewById(R.id.btn_order_pay);
        }
    }
}
